/*
** utils.c for  in /home/lund/Projets/SysUnixAv/seutrace
** 
** Made by florian dewulf
** Login   <dewulf_f@epitech.net>
** 
** Started on  Wed May  1 17:18:17 2013 florian dewulf
** Last update Fri May 17 17:41:21 2013 kevin lansel
*/

#include	<stdio.h>
#include	<stdlib.h>
#include	"strace.h"
#include	"syscall.h"

int		is_syscall(unsigned long long rax)
{
  int		i;
  int		j;

  i = 0;
  j = 0;
  while (tab_syscall[i].id < 312)
    i++;
  while (j < i + 1)
    {
      if (tab_syscall[j].id == (int)rax)
	return (1);
      j++;
    }
  return (0);
}

int		size_tab(int *tab)
{
  int		i;

  i = 0;
  while (tab[i] != VOID)
    i++;
  return (i);
}

t_syscall	which_syscall(unsigned long long rax)
{
  int		i;
  int		j;

  i = 0;
  j = 0;
  while (tab_syscall[i].id < 312)
    i++;
  while (j < i + 1)
    {
      if (tab_syscall[j].id == (int)rax)
	return (tab_syscall[j]);
      j++;
    }
  return (tab_syscall[0]);
}

void		*xmalloc(int size)
{
  void		*ptr;

  ptr = malloc(size);
  if (ptr == NULL)
    {
      printf("xmalloc fail\n");
      exit(EXIT_FAILURE);
    }
  return (ptr);
}

void		fill_tab(unsigned long long *tab, t_syscall *sys,
			 struct user_regs_struct *reg)
{
  unsigned long	int	buffer[6];
  int			i;

  i = 0;
  buffer[0] = reg->rdi;
  buffer[1] = reg->rsi;
  buffer[2] = reg->rdx;
  buffer[3] = reg->rcx;
  buffer[4] = reg->r8;
  buffer[5] = reg->r9;
  while (i < size_tab(sys->arg))
    {
      tab[i] = buffer[i];
      i++;
    }
  tab[i] = VOID;
}
